<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>


var maxAreaOfIsland = function(grid) {
    // 1. 笨方法
    // var islands = []
    // for (var i = 0, length1 = grid.length; i < length1; i++) {
    //     for(var j = 0, length2 = grid[i].length; j < length2; j++) {
    //         if (grid[i][j] === 1) {
    //             islands.push(i + ',' + j)
    //         }
    //     }
    // }
    // function getSize (originModule, index = 0) {
    //     islands.splice(index, 1)
    //     var originArr = originModule.split(',')
    //     var one = originArr[0]
    //     var two = originArr[1]
    //     var size = 1

    //     let index1 = islands.indexOf((+one - 1) + ',' + two)
    //     if ( index1 !== -1) {
    //         size += getSize((+one - 1) + ',' + two, index1)
    //     }
    //     var index2 = islands.indexOf((+one + 1) + ',' + two) 
    //     if (index2 !== -1) {
    //         size += getSize((+one + 1) + ',' + two, index2)
    //     }
    //     var index3 = islands.indexOf(one + ',' + (+two - 1))
    //     if (index3 !== -1) {
    //         size += getSize(one + ',' + (+two - 1), index3)
    //     }

    //     var index4 = islands.indexOf(one + ',' + (+two + 1))
    //     if (index4 !== -1) {
    //         size += getSize(one + ',' + (+two + 1), index4)
    //     }
    //     return size
    // }
    // var maxSize = 0
    // while(islands.length) {
    //     var oneSize = getSize(islands[0])
    //     maxSize = Math.max(maxSize, oneSize)
    // }
    // return maxSize


    // 2. 计算过的岛屿归零
    let maxSize = 0
    for(let i = 0, length1 = grid.length; i < length1; i++) {
        for (let j = 0, length2 = grid[i].length; j < length2; j++) {
            if (grid[i][j] === 1) {
                maxSize = Math.max(getSize(i, j), maxSize)
            }
        }
    }
    function getSize (i, j) {
        let size = 1
        grid[i][j] = 0
        if (grid[i - 1] && grid[i - 1][j] === 1) {
            size += getSize(i - 1, j)
        }
        if (grid[i + 1] && grid[i + 1][j] === 1) {
            size += getSize(i + 1, j)
        }
        if (grid[i] && grid[i][j - 1] === 1) {
            size += getSize(i, j - 1)
        }
        if (grid[i] && grid[i][j + 1] === 1) {
            size += getSize(i, j + 1)
        }

        return size
    }
    return maxSize
};

var grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
// var grid = [[1,1,0,0,0],[1,1,0,0,0],[0,0,0,1,1],[0,0,0,1,1]]

console.log('maxAreaOfIsland(grid):', maxAreaOfIsland(grid))



</script>
</body>
</html>